package com.hello.controller;


import com.hello.dto.*;
import com.hello.dto.UserDTO;
import com.hello.entity.Comments;
import com.hello.entity.Post;
import com.hello.result.Result;
import com.hello.service.*;

import com.hello.vo.PostVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RestController
@RequestMapping("/admin/post")
@Slf4j
@Api(tags="宠物圈相关接口")
public class PostController {

    @Resource
    private IPostService postService;
    @Resource
    private IUserService userService;
    @Resource
    private IPLikesService pLikesService;
    @Resource
    private ICommentsService commentsService;
    @Resource
    private IFollowService followService;

    @PostMapping("/addPost")
    @ApiOperation(value = "添加新帖子")
    public Result addPost(@RequestBody PostAddDTO postAddDTO) {
        log.info("新增帖子:{}",postAddDTO);
        postService.addPost(postAddDTO);
        return Result.success();

    }

    @PostMapping("/LikedPosts/{user_id}")
    @ApiOperation(value="我赞过的")
    public Result LikedPosts(@PathVariable Long user_id) {
        List<PostLikeDTO> postLikeDTOS = postService.getLikedList(user_id);
        return Result.success(postLikeDTOS);
    }

    @PostMapping("/petCircle/{user_id}")
    @ApiOperation(value = "宠物圈")
    public Result petCircle(@PathVariable Long user_id){
        List<Post> postList= postService.queryPostList();
        List<PostVO> list = new ArrayList<>();
        for(Post post:postList){
            Long host_id = post.getHostId();
            Integer post_order = post.getPostOrder();
            UserDTO userDTO = userService.searchById(host_id);
            PostVO postVO = PostVO.builder()
                    .hostId(host_id)
                    .postOrder(post_order)
                    .userName(userDTO.getUsername())
                    .headshotUrl(userDTO.getHeadshotUrl())
                    .postTime(post.getPostTime())
                    .text(post.getText())
                    .imageUrl(post.getImageUrl())
                    .liked(pLikesService.haveLiked(host_id,post_order,user_id))
                    .likes(pLikesService.getLikesCount(host_id,post_order))
                    .comments(commentsService.getCommentCount(host_id,post_order))
                    .shares(post.getForwardingNum())
                    .followed(followService.haveFollowed(user_id,host_id))
                    .build();
            list.add(postVO);
        }
        return Result.success(list);
    }

    @PostMapping("/MyFollowedPetCircle/{user_id}")
    @ApiOperation(value = "我关注的宠物圈")
    public Result MyFollowedPetCircle(@PathVariable Long user_id){
        //List<Post> postList= postService.queryPostList();
        List<Post> postList= postService.getFollowedPost(user_id);
        List<PostVO> list = new ArrayList<>();
        for(Post post:postList){
            Long host_id = post.getHostId();
            Integer post_order = post.getPostOrder();
            UserDTO userDTO = userService.searchById(host_id);
            PostVO postVO = PostVO.builder()
                    .hostId(host_id)
                    .postOrder(post_order)
                    .userName(userDTO.getUsername())
                    .headshotUrl(userDTO.getHeadshotUrl())
                    .postTime(post.getPostTime())
                    .text(post.getText())
                    .imageUrl(post.getImageUrl())
                    .liked(pLikesService.haveLiked(host_id,post_order,user_id))
                    .likes(pLikesService.getLikesCount(host_id,post_order))
                    .comments(commentsService.getCommentCount(host_id,post_order))
                    .shares(post.getForwardingNum())
                    .followed(followService.haveFollowed(user_id,host_id))
                    .build();
            list.add(postVO);
        }
        return Result.success(list);
    }

    @PostMapping("/addComment")
    @ApiOperation("评论")
    public Result addComment(@RequestBody CommentsDTO commentsDTO){
        boolean result = commentsService.addComment(commentsDTO);
        if(result)
            return Result.success();
        else
            return Result.error("评论失败");
    }

    @PostMapping("/addPLike")
    @ApiOperation("点赞")
    public  Result addPLike(@RequestBody PLikeDTO pLikeDTO){
        boolean result = pLikesService.addPLikes(pLikeDTO);
        if(result)
            return Result.success();
        else
            return Result.error("点赞失败");
    }

    @PostMapping("/deletePLike")
    @ApiOperation("取消点赞")
    public  Result deletePLike(@RequestBody PLikeDTO pLikeDTO){
        boolean result = pLikesService.deletePLikes(pLikeDTO);
        if(result)
            return Result.success(pLikeDTO);
        else
            return Result.error("取消点赞失败");
    }

    @PostMapping("/addFollow")
    @ApiOperation("关注")
    public Result addFollow(@RequestBody FollowDTO followDTO){
        boolean result = followService.addFollow(followDTO);
        if(result)
            return Result.success();
        else
            return Result.error("关注失败");
    }

    @PostMapping("/deleteFollow")
    @ApiOperation("取消关注")
    public Result deleteFollow(@RequestBody FollowDTO followDTO){
        boolean result = followService.deleteFollow(followDTO);
        if(result)
            return Result.success();
        else
            return Result.error("取消关注失败");
    }

    @PostMapping("/postComments")
    @ApiOperation("评论信息")
    public Result postComments(@RequestBody PostSearchDTO postSearchDTO){

        List<UserCommentsDTO> list = commentsService.postComments(postSearchDTO);
        for(UserCommentsDTO userCommentsDTO:list){
            UserDTO userDTO = userService.searchById(userCommentsDTO.getCommenterId());
            userCommentsDTO.setUsername(userDTO.getUsername());
        }

        if(!list.isEmpty()) {

            return Result.success(list);
        }
        else return Result.error("no comments");
    }


}
